﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using COMOSOFT.Class;
using COMOSOFT.Production.Forms;
using DevExpress.XtraGrid.Views.Grid;

namespace COMOSOFT.Production.Forms
{
    public partial class FrmDeliveryGaneral : Form
    {
        Task tak;       
        object year,month;
        bool alow = true;
        FrmDeliveryView frDV = null;
        ClassShare cls = new ClassShare("FrmDeliveryGaneral");
        Dictionary<int, int> listPO;
        FrmPreViewDeliveryGaneral frmPV = null;
        public FrmDeliveryGaneral()
        {
            InitializeComponent();
        }

        #region[LoadData]
        public void LoadGeneral(object Y, object M)
        {
            try
            {
                this.year = Y;
                this.month = M;
                //radioGroup_Delivery.Properties.Items[0].Description = this.year.ToString();
                if (tak == null || tak.Status != TaskStatus.Running)
                {
                    splashScreenManager1.ShowWaitForm();
                    gridControl_DeliveryGaneral.DataSource = null;
                    tak = Task.Factory.StartNew(() => LoadData());
                    tak.ContinueWith(tt => FillTable());
                }
                bandedGridView_DeliveryGaneral.ColumnPanelRowHeight = 25;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        private delegate void dlgFillTable();
        private void FillTable()
        {
            try
            {
                if (InvokeRequired)
                {
                    this.Invoke(new dlgFillTable(FillTable));
                }
                else
                {
                    gridControl_DeliveryGaneral.DataSource = dataSet_Delivery.sp_DeliveryGaneral;
                    splashScreenManager1.CloseWaitForm();
                }
            }
            catch
            {
                MessageBox.Show("Progess fail");
            }
        }

        private void LoadData()
        {
            sp_DeliveryGaneralTableAdapter.Fill(dataSet_Delivery.sp_DeliveryGaneral, year, month);
        }               
        #endregion

        #region [Add Delivery Details]
        private void addDeliveryDetailsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {               
                if (alow == true)
                {
                    listPO = new Dictionary<int, int>();
                    GridView view = bandedGridView_DeliveryGaneral as GridView;
                    int[] arrItem = view.GetSelectedRows();
                    foreach (int j in arrItem)
                    {
                        listPO.Add(Convert.ToInt32(view.GetDataRow(j)["OID"]), Convert.ToInt32(view.GetDataRow(j)["ShipQty"]));
                    }
                    frDV = new FrmDeliveryView(-1) { MdiParent = ClassShare.frmain, Text = "Add New Delivery" };
                    frDV.SetValue(listPO);
                    frDV.Show();
                    frDV.AddNew();
                    frDV.LoadData();
                    ClassShare.frDV = frDV;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        #endregion

        private void bandedGridView_DeliveryGaneral_RowCellClick(object sender, DevExpress.XtraGrid.Views.Grid.RowCellClickEventArgs e)
        {
            try
            {
                if (e.Column.Caption.ToString() == "Act Ship Date")
                {
                    GridView view = bandedGridView_DeliveryGaneral as GridView;
                    int[] arrItem = view.GetSelectedRows();
                    int index = arrItem[0];
                    DateTime date = Convert.ToDateTime(view.GetDataRow(index)["ActShipDate"]);
                    for (int i = 1; i < arrItem.Count(); i++)
                    {
                        if (date != Convert.ToDateTime(view.GetDataRow(i)["ActShipDate"]))
                        {
                            alow = false;
                            break;
                        }
                    }
                }
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        public void Preview()
        {
            if (frmPV != null && !frmPV.IsDisposed)
            {
                frmPV.Select();
                frmPV.LoadData();
            }
            else
            {
                frmPV = new FrmPreViewDeliveryGaneral(bandedGridView_DeliveryGaneral);
                frmPV.LoadData();
                frmPV.Show();
            }
        }
    }
}